import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testlimitrange:

    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/limitrange/1_get_limitrange_list.yaml'))
    def test_get_limitrange_list(self, limitrangeinfo, get_env_info):
        # 获取limitrange列表接口
        baseUrl = get_env_info
        url = baseUrl + limitrangeinfo['request']['url'].replace('{namespace}','default')
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/limitrange/2_create_limitrange.yaml'))
    def test_create_limitrange(self, limitrangeinfo, get_env_info):
        # 创建limitrange接口
        baseUrl = get_env_info
        url = baseUrl + limitrangeinfo['request']['url'].replace('{namespace}','default')
        method = limitrangeinfo['request']['method']
        data = limitrangeinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in limitrangeinfo['name']:
            limitrangename=req.json()['metadata']['name']
            globals()['limitrangename']=limitrangename
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/limitrange/3_get_specific_limitrange.yaml'))
    def test_get_specific_limitrange(self, limitrangeinfo, get_env_info):
        # 获取指定limitrange接口
        baseUrl = get_env_info
        url = baseUrl + limitrangeinfo['request']['url'].replace('{namespace}','default').replace('{limitrangename}',globals()['limitrangename'])
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in limitrangeinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/limitrange/4_update_limitrange.yaml'))
    def test_update_limitrange(self, limitrangeinfo, get_env_info):
        # 更新指定limitrange接口
        baseUrl = get_env_info
        url = baseUrl + limitrangeinfo['request']['url'].replace('{namespace}','default').replace('{limitrangename}',globals()['limitrangename'])
        method = limitrangeinfo['request']['method']
        limitrangeinfo['request']['data'] = globals()['updatereqbody']
        data = limitrangeinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/limitrange/5_update_section_limitrange.yaml'))
    def test_update_section_limitrange(self, limitrangeinfo, get_env_info):
        # 部分更新指定limitrange的接口
        baseUrl = get_env_info
        url = baseUrl + limitrangeinfo['request']['url'].replace('{namespace}','default').replace('{limitrangename}',globals()['limitrangename'])
        method = limitrangeinfo['request']['method']
        data = limitrangeinfo['request']['data']
        header = limitrangeinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/limitrange/6_delete_specific_limitrange.yaml'))
    def test_delete_specific_limitrange(self, limitrangeinfo, get_env_info):
        # 删除limitrange接口
        baseUrl = get_env_info
        url = baseUrl + limitrangeinfo['request']['url'].replace('{namespace}','default').replace('{limitrangename}',globals()['limitrangename'])
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']